package com.kungfuhacking.wristbandpro.location.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.timesquare.CalendarPickerView;

import java.util.Calendar;
import java.util.Date;

/**
 * 选择日期页面
 * Created by Aaron on 17/5/9.
 */

public class ChooseDateActivity extends BaseActivity {

    private Calendar nextYear;
    private Calendar lastYear;
    protected long defaultTime = 0;// 默认的时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_date);

        initWidget();
    }

    private void initWidget() {
        CalendarPickerView calendar_view = (CalendarPickerView) findViewById(R.id.calendar_view);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initDefaultDate();
        calendar_view.init(lastYear.getTime(), nextYear.getTime())
                .withSelectedDate(new Date()).inMode(CalendarPickerView.SelectionMode.SINGLE);

        calendar_view.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {

                Calendar c = Calendar.getInstance();
                c.setTime(date);

                Date time = c.getTime();
//                int day = c.get(Calendar.DAY_OF_MONTH);
//                int month = c.get(Calendar.MONTH);
//
//                LogUtil.d("TAG", "选择的日期:::===>" + (month + 1) + "月" + day + "日");

                Intent data = new Intent();
                data.putExtra("showDate", (c.get(Calendar.MONTH) + 1) + "月" + c.get(Calendar.DAY_OF_MONTH) + "日" );
                data.putExtra("date", time.getTime());

                setResult(0x33334, data);
                finish();

            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });
    }

    private void initDefaultDate() {

        // 日历 初始化
        nextYear = Calendar.getInstance();
        nextYear.add(Calendar.DAY_OF_WEEK, 1);

        lastYear = Calendar.getInstance();
        lastYear.add(Calendar.DAY_OF_WEEK, -6);
        // 需要一个默认值 当天时间向前推两个小时
        Calendar instance = Calendar.getInstance();

        defaultTime = instance.getTime().getTime();

    }
}
